package io.github.wslxm.springbootplus2.starter.websocket.util;


import io.github.wslxm.springbootplus2.starter.redis.constant.CacheModeConst;
import io.github.wslxm.springbootplus2.starter.redis.constant.CacheSeparateConst;
import io.github.wslxm.springbootplus2.starter.websocket.constant.WebsocketConst;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class WebsocketUtil {

    @Value("${spring.redis.cacheMode:singlenode}")
    protected String cacheMode;

    /**
     * 当前项目模块名称, 作用于 redis 前缀
     */
    @Value("${spring.application.name:no-project-name}")
    protected String moduleName;


    /**
     * 获取缓存 key
     * @return
     */
    public String getCacheKey() {
        // 使用添加了自定义前缀
        String websocketCacheKey = WebsocketConst.WEBSOCKET_CACHE_KEY;
        // 是否启用多节点模式
        if (cacheMode.equals(CacheModeConst.MULTINODE)) {
            websocketCacheKey = moduleName + CacheSeparateConst.SEPARATE + websocketCacheKey;
        }
        return websocketCacheKey;
    }
}
